package com.ruoyi.pay.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.ruoyi.common.component.FileUploadComponent;
import com.ruoyi.common.component.FileUploadResponse;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.utils.BaiduOcrApiClient;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pay.domain.Qrcode;
import com.ruoyi.pay.service.IQrcodeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 收款码管理Controller
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@Controller
@RequestMapping("/pay/qrcode")
public class QrcodeController extends BaseController
{
    private String prefix = "pay/qrcode";

    @Autowired
    private IQrcodeService qrcodeService;
    @Autowired
    private FileUploadComponent fileUploadComponent;


    @RequiresPermissions("pay:qrcode:view")
    @GetMapping()
    public String qrcode()
    {
        return prefix + "/qrcode";
    }

    /**
     * 查询收款码管理列表
     */
    @RequiresPermissions("pay:qrcode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Qrcode qrcode)
    {
        startPage();
        List<Qrcode> list = qrcodeService.selectQrcodeList(qrcode);
        return getDataTable(list);
    }

    /**
     * 导出收款码管理列表
     */
    @RequiresPermissions("pay:qrcode:export")
    @Log(title = "收款码管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Qrcode qrcode)
    {
        List<Qrcode> list = qrcodeService.selectQrcodeList(qrcode);
        ExcelUtil<Qrcode> util = new ExcelUtil<Qrcode>(Qrcode.class);
        return util.exportExcel(list, "收款码管理数据");
    }

    /**
     * 新增收款码管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存收款码管理
     */
    @RequiresPermissions("pay:qrcode:add")
    @Log(title = "收款码管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Qrcode qrcode, MultipartFile file)
    {
        if(file != null)
        {
            try {
                InputStream inputStream = file.getInputStream();
                Qrcode qrcode1 = BaiduOcrApiClient.basicGeneral(inputStream);
                if(StringUtils.isNotBlank(qrcode1.getQrType()))
                {
                    qrcode.setQrType(qrcode1.getQrType());
                }
                if(null != qrcode1.getQrPrice())
                {
                    qrcode.setQrPrice(qrcode1.getQrPrice());
                }
                if(StringUtils.isBlank(qrcode1.getQrContent()))
                {
                    return AjaxResult.error("无法识别图片中的内容");
                }
                qrcode.setQrContent(qrcode1.getQrContent());
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
        }
        if(StringUtils.isBlank(qrcode.getQrContent()) || null == qrcode.getQrPrice() || 0L == qrcode.getQrPrice().longValue())
        {
            return AjaxResult.error("付款码内容和金额不能为空或0");
        }

        int count = qrcodeService.insertQrcode(qrcode);
        if(count > 0)
        {
            if(file != null)
            {
                try {
                    FileUploadResponse response = fileUploadComponent.upload("pay",file);
                    qrcode.setQrUrl(response.getPathFileName());
                    count = qrcodeService.updateQrcode(qrcode);
                } catch (IOException e) {
                    count = 0;
                }
            }
        }

        return toAjax(count);
    }

    /**
     * 修改收款码管理
     */
    @RequiresPermissions("pay:qrcode:edit")
    @GetMapping("/edit/{qrId}")
    public String edit(@PathVariable("qrId") Long qrId, ModelMap mmap)
    {
        Qrcode qrcode = qrcodeService.selectQrcodeByQrId(qrId);
        mmap.put("qrcode", qrcode);
        return prefix + "/edit";
    }

    /**
     * 修改保存收款码管理
     */
    @RequiresPermissions("pay:qrcode:edit")
    @Log(title = "收款码管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Qrcode qrcode)
    {
        return toAjax(qrcodeService.updateQrcode(qrcode));
    }

    /**
     * 删除收款码管理
     */
    @RequiresPermissions("pay:qrcode:remove")
    @Log(title = "收款码管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qrcodeService.deleteQrcodeByQrIds(ids));
    }
}
